package net.zjitc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.mapper.GoodsMapper;
import net.zjitc.pojo.Goods;
import net.zjitc.pojo.Pages;
import net.zjitc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods findGoodsById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public Integer addGoods(Goods good) {
        goodsMapper.insert(good);
        return good.getId();
    }

    @Override
    public void delGoods(Integer id) {
        goodsMapper.deleteById(id);
    }

    @Override
    public void updateGoods(Goods good) {
        goodsMapper.update(good);
    }

    @Override
    public Map<String, Object> getGoodsList(Pages pages) {
        // 获取总记录数
        long total = goodsMapper.selectCount(null);
        pages.setTotal(total);
        // 创建分页对象
        Page<Goods> page =new Page<>(pages.getPage(),pages.getPagesize());
        // 查询分页数据
        Page<Goods> goodsPage = goodsMapper.selectPage(page, null);
        // 构建返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        System.out.println("goodsPage = " + goodsPage.getRecords());
        map.put("list", goodsPage.getRecords());
        return map;
    }



}
